
/**
* @module websocket
* @author: Hjava
* @description: WebSocket管理器
* @since: 2018-07-15 22:48:51
*/
Object.defineProperty(exports, "__esModule", { value: true });
var analytics_1 = __webpack_require__(/*! _util/analytics */ "./src/core/utils/analytics.ts");
var userData_1 = __webpack_require__(/*! _util/userData */ "./src/core/utils/userData.ts");
var uuid_1 = __webpack_require__(/*! _util/uuid */ "./src/core/utils/uuid.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/core/utils/log.ts");
var MWebSocket = /** @class */ (function () {
    function MWebSocket() {
        this._socket = null;
        this._version = 0;
        this._startTime = 0;
        this.url = '';
        this.sessionId = '';
    }
    MWebSocket.prototype.connect = function (url) {
        this.url = url;
        this.sessionId = uuid_1.default.next();
        this._closeSocket();
        this._socket = new WebSocket(this.url);
        this._socket.binaryType = 'arraybuffer';
        this._socket.version = this._version++;
        this._startTime = Date.now();
        var valObject = {
            host: this.url,
            net: navigator.onLine ? 1 : 0,
            websocketId: this.sessionId
        };
        log_1.default.log('MWebSocket::connect info:' + JSON.stringify(valObject));
        analytics_1.default.report({
            nm: 'login_websocket_start',
            val: valObject
        });
    };
    MWebSocket.prototype.disconnect = function () {
        if (this._socket && this._socket.readyState !== WebSocket.CLOSED) {
            this._closeSocket();
        }
    };
    MWebSocket.prototype.send = function (data) {
        if (!this._socket || (this._socket.readyState !== WebSocket.OPEN)) {
            this.disconnect();
            return;
        }
        try {
            this._socket.send(data);
        }
        catch (e) {
            log_1.default.error(e);
        }
    };
    MWebSocket.prototype.getSocketId = function () {
        return this.sessionId;
    };
    MWebSocket.prototype._closeSocket = function () {
        if (this._socket) {
            try {
                this._socket.close();
                // 避免由于网络原因导致后端没有收到关闭帧（close frame），从而继续像旧通道推送数据
                this._socket.onMessage = function (message) {
                    // do nothing
                };
                var valObject = {
                    websocketId: this.sessionId,
                    net: navigator.onLine ? 1 : 0,
                    host: this.url
                };
                log_1.default.log('MWebSocket::_closeSocket info:' + JSON.stringify(valObject));
                analytics_1.default.report({
                    nm: 'login_websocket_close',
                    val: valObject
                });
            }
            catch (e) {
                log_1.default.error(e);
            }
        }
        this._socket = null;
    };
    MWebSocket.prototype.onOpen = function (func) {
        var _this = this;
        this._socket.onopen = function (e) {
            var valObject = {
                websocketId: _this.sessionId,
                time: Date.now() - _this._startTime,
                host: _this.url
            };
            analytics_1.default.report({
                nm: 'login_websocket_success',
                val: valObject
            });
            userData_1.default.set('websocketId', _this.sessionId);
            var env = userData_1.default.get('env');
            log_1.default.log('MWebSocket::onOpen id:' + _this.sessionId + (" \u5F53\u524D WebSocket \u5730\u5740: " + _this.url + "   \u5F53\u524D\u4F7F\u7528\u73AF\u5883:" + userData_1.default.get('env')));
            func(_this.sessionId);
        };
    };
    MWebSocket.prototype.onMessage = function (func) {
        this._socket.onmessage = function (message) {
            func(message.data);
        };
    };
    MWebSocket.prototype.onClose = function (func) {
        var _this = this;
        this._socket.onclose = function (e) {
            var closeLog = 'MWebSocket::onClose id:' + _this.sessionId + ' url:' + _this.url;
            try {
                if (e && e.code) {
                    closeLog += ' code:' + e.code;
                }
                if (e && e.reason) {
                    closeLog += ' reason:' + e.reason;
                }
                if (e && e.type) {
                    closeLog += ' type:' + e.type;
                }
            }
            catch (err) {
                log_1.default.log(err);
            }
            log_1.default.log(closeLog);
            analytics_1.default.report({
                nm: 'login_websocket_error',
                val: {
                    code: e ? e.code : -1,
                    websocketId: _this.sessionId,
                    net: navigator.onLine ? 1 : 0,
                    result: e && e.wasClean ? 1 : 0,
                    host: _this.url
                }
            });
            func(_this.sessionId);
        };
    };
    MWebSocket.prototype.onError = function (func) {
        var _this = this;
        this._socket.onerror = function (e) {
            var errorLog = 'MWebSocket::onError id:' + _this.sessionId + ' url:' + _this.url;
            try {
                if (e && e.errMsg) {
                    errorLog += ' errMsg:' + e.code;
                }
            }
            catch (err) {
                log_1.default.log(err);
            }
            log_1.default.log(errorLog);
            func(_this.sessionId);
        };
    };
    return MWebSocket;
}());
exports.default = MWebSocket;


//# sourceURL=webpack://MTDX/./src/core/login/websocket.ts?